import {Cart} from "../../models/cart";
import {Calculator} from "../../models/calculator";
import {SpuPaging} from "../../models/spu-paging";

const cart = new Cart()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartItems: [],
        isEmpty: false,
        allChecked: false,
        totalPrice: 0,
        totalSkuCount: 0,
        spuPaging:{},
        loadType:'loading'

    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        await cart.getCartItemsFromServer()
        // 瀑布流数据
        await this.initBottomSpuList()
    },

    async initBottomSpuList() {
        const paging = SpuPaging.getSpuPaging()
        this.data.spuPaging = paging
        const data = await paging.getMoreData()
        if (!data) return
        wx.lin.renderWaterFlow(data.items)
    },

    empty() {
        this.setData({
            isEmpty: true,
        })
        wx.hideTabBarRedDot({
            index: 2
        })
    },

    notEmpty() {
        this.setData({
            isEmpty: false
        })
        wx.showTabBarRedDot({
            index: 2
        })
    },

    isAllChecked() {
        const allChecked = cart.isAllChecked()
        this.setData({
            allChecked
        })
    },

    onDeleteItem() {
        this.isAllChecked()
        this.refreshCartData()
    },

    onSingleCheck() {
        this.isAllChecked()
        this.refreshCartData()
    },

    onCountFloat() {
        this.refreshCartData()
    },

    onCheckAll(event) {
        const checked = event.detail.checked
        cart.checkedAll(checked)
        this.setData({
            cartItems: this.data.cartItems
        })
        this.refreshCartData()
    },

    // 计算总价和数量
    refreshCartData() {
        const cartItems = cart.getAllCheckedItem()
        const calculator = new Calculator(cartItems)
        this.setData({
            totalPrice: calculator.totalPrice,
            totalSkuCount: calculator.totalSkuCount
        })
    },

    onSettle(event) {
        if (this.data.totalSkuCount <= 0) return
        wx.navigateTo({
            url: '/pages/order/order'
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     * 每次打开页面都会执行
     */
    onShow: function () {
        const cartItems = cart.getAllCartItemFromLocal().items
        if (cart.isEmpty()) {
            this.empty()
            return
        }
        this.setData({
            cartItems
        });
        this.notEmpty()
        this.isAllChecked()
        this.refreshCartData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: async function () {
        const data = await this.data.spuPaging.getMoreData()
        if (!data) return
        wx.lin.renderWaterFlow(data.items)
        if (!data.hasMore) {
            this.setData({
                loadType:'end'
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
